package com.spring.authManage.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.spring.authManage.dao.ResourceInfoDao;
import com.spring.authManage.pojo.ResourceInfo;
import com.spring.authManage.pojo.ResourceInfoParam;
import com.spring.authManage.service.ResourceInfoService;
import com.spring.common.util.ChunQiuTool;

@Service("resourceInfoService")
public class ResourceInfoServiceImpl implements ResourceInfoService{

	@Autowired
	private ResourceInfoDao resourceInfoDao;
	
	/**
	 * 添加资源
	 * @param resourceInfo
	 * @return
	 */
	public String addResource(ResourceInfo resourceInfo) throws Exception {	
		resourceInfo = calcResourceElements(resourceInfo);
		String error = checkform(resourceInfo);
		String result = "";
		if(error.equals("")){
			this.resourceInfoDao.addResource(resourceInfo);
			result = "Y";
		} else {
			result = error;
		}
		return result;
	}
	/**
	 * 更改资源信息
	 * @param resourceInfo
	 * @return
	 */
	public String updateResource(ResourceInfo resourceInfo) throws Exception {
		resourceInfo = calcResourceElements(resourceInfo);
		String error = checkform(resourceInfo);
		String result = "";
		if(error.equals("")){
			this.resourceInfoDao.updateResource(resourceInfo);
			result = "Y";
		} else {
			result = error;
		}
		return result;
	}
	/**
	 * 作废资源
	 * @param resourceInfo
	 * @return
	 */
	public void deleteResource(String id) throws Exception {
		this.resourceInfoDao.deleteResource(id);
	}
	/**
	 * 重启资源
	 * @param resourceInfo
	 * @return
	 */
	public void restartResourceInfo(String id) throws Exception {
		this.resourceInfoDao.restartResourceInfo(id);
	}
	/**
	 * 查询是否已存在相同资源
	 * @param resourceInfo
	 * @return
	 * @throws Exception 
	 */
	public Integer existResource(ResourceInfo resourceInfo) throws Exception {
		Integer resInt = this.resourceInfoDao.existResource(resourceInfo);
		return resInt;
	}
	/**
	 * 计算分页
	 * @param resourceInfo
	 * @return
	 * @throws Exception 
	 */
	public Integer countResourceByParam(ResourceInfoParam resourceInfoParam) throws Exception {
		Integer resInt = this.resourceInfoDao.countResourceByParam(resourceInfoParam);
		return resInt;
	}
	/**
	 * 根据条件查询资源
	 * @param resourceInfo
	 * @return
	 * @throws Exception 
	 */
	public List<ResourceInfo> queryResourceByParam(ResourceInfoParam resourceInfoParam) throws Exception {
		List<ResourceInfo> resourceInfoFirst = this.resourceInfoDao.queryResourceByParam(resourceInfoParam);
		return resourceInfoFirst;
 	}
	/**
	 * 进去资源管理页面初始查询所有一级菜单
	 * 
	 * @return
	 */
	public List<ResourceInfo> queryFirstLevel(ResourceInfoParam param) throws Exception {
		List<ResourceInfo> resourceInfoList = this.resourceInfoDao.queryFirstLevel(param);
		return resourceInfoList;
	}
	/**
	 * 点击一级菜单查询附属二级菜单详细
	 * @param id
	 * @return
	 */
	public List<ResourceInfo> querySecondLevel(String id) throws Exception {
		List<ResourceInfo> resourceInfoList = this.resourceInfoDao.querySecondLevel(id);
		return resourceInfoList;	
	}

	/**
	 * 根据角色获取该角色拥有的权限
	 * @param roleId
	 * @return
	 */
	public List<ResourceInfo> getResourceByRole(String roleId) throws Exception {
		List<ResourceInfo> resourceInfoList = this.resourceInfoDao.getResourceByRole(roleId);
		return resourceInfoList;	
	}
	/**
	 * 根据用户获取用户所拥有的权限
	 * @param userId
	 * @return
	 */
	public List<ResourceInfo> getResourceByUser(String userId) throws Exception {
		List<ResourceInfo> resourceInfoList = this.resourceInfoDao.getResourceByUser(userId);
		return resourceInfoList;	
	}
	/**
	 * 根据ID获取权限具体信息
	 * @param id
	 * @return
	 */
	public ResourceInfo queryResourceById(String id) throws Exception {
		ResourceInfo resourceInfo = this.resourceInfoDao.queryResourceById(id);
		return resourceInfo;
	}
	/**
	 * 查询所有父资源name,id
	 * @param id
	 * @return
	 */
	public List<ResourceInfo> queryParentResource() throws Exception {
		List<ResourceInfo> resourceInfoList = this.resourceInfoDao.queryParentResource();
		return resourceInfoList;
	}
	/**
	 * 根据父资源name查询id
	 * @param id
	 * @return
	 */
	public List<ResourceInfo> getParentId(String id) throws Exception {
		List<ResourceInfo> resourceInfoList = this.resourceInfoDao.getParentId(id);
		return resourceInfoList;
	}
	/**
	 * 综合方法 调用getParentResource()获取所有父资源id,name；拼接加上跟菜单id=-1
	 * @param id
	 * @return
	 */
	public List<ResourceInfo> queryAllParentResource() throws Exception {
		// 父资源下拉框
		List<ResourceInfo> parentInfo = queryParentResource();
		// 设定根菜单 对应一级资源
		ResourceInfo firstInfo = new ResourceInfo();
		firstInfo.setParentId("-1");
		firstInfo.setParentName("根目录");
		parentInfo.add(0, firstInfo);
		return parentInfo;
	}
	/**
	 * 对每个resourceInfo进行值处理
	 * @param id
	 * @return
	 */
	private ResourceInfo calcResourceElements(ResourceInfo resourceInfo) throws Exception{
		resourceInfo.obtainType();// 根据前端插件改type null为页面1，on为按钮2
		resourceInfo.obtainLevel();// 根据parentId设置level值 parentId为-1level为1，否则为2
		return resourceInfo;	
	}
	/**
	 * 格式检查后台，检查是否有重复资源
	 * @param resourceInfo
	 * @return
	 */
	private String checkform(ResourceInfo resourceInfo) throws Exception {
		String error = "";
		if ("".equals(ChunQiuTool.convertNull(resourceInfo.getName()))) {
			error = "名称不能为空！";
		} else if (ChunQiuTool.convertNull(resourceInfo.getName()).length() > 50) {
			error = "名称不能长于50！";
		} else if ("".equals(ChunQiuTool.convertNull(resourceInfo.getUrl()))) {
			error = "URL不能为空！";
		} else if (ChunQiuTool.convertNull(resourceInfo.getUrl()).length() > 50) {
			error = "URL不能长于50！";
		} else if ("".equals(ChunQiuTool.convertNull(resourceInfo.getType()))) {
			error = "类型不能为空！";
		} else if (ChunQiuTool.convertNull(resourceInfo.getMemo()).length() > 200) {
			error = "备注不能长于200！";
		} else if ("".equals(ChunQiuTool.convertNull(resourceInfo.getParentId()))) {
			error = "父资源不能为空！";
		} else {
			Integer checkDouble = this.resourceInfoDao.existResource(resourceInfo);
			if(checkDouble != 0 ) {
				error = "资源重名";
			}
		}
		return error;
	}
	
}
